package com.ib.review.util;

import cn.hutool.core.codec.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.Key;

/**
 * 3DES加密工具类
 *
 */
public class DES3Util {
    // 密钥
    private final static String SECRETKEY = "INbSvyvOXkSkcWNSc8HpHIa4";
    // 向量
    private final static String IV = "drS66lwy";
    // 加解密统一使用的编码方式
    private final static String encoding = "utf-8";

    /**
     * 3DES加密
     *
     * @param plainText
     *            普通文本
     * @return
     */
    public static String encrypt(String plainText) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(SECRETKEY.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
        return Base64.encode(encryptData);
    }

    /**
     * 3DES解密
     *
     * @param encryptText
     *            加密文本
     * @return
     */
    public static String decrypt(String encryptText) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(SECRETKEY.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
        byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));

        return new String(decryptData, encoding);
    }

    /**
     * 3DES加密
     *
     * @param secretKey
     *            秘钥
     * @param iv
     *            偏移向量
     * @param plainText
     *            普通文本
     * @return
     * @throws Exception
     */
    public static String encryptString(String secretKey, String iv,
                                       String plainText) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
        return Base64.encode(encryptData);
    }

    /**
     * 3DES解密
     *
     * @param secretKey
     *            秘钥
     * @param iv
     *            偏移向量
     * @param encryptText
     *            密文
     * @return
     * @throws Exception
     */
    public static String decryptString(String secretKey, String iv,
                                       String encryptText) throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);

        byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));

        return new String(decryptData, encoding);
    }

    /**
     * 3DES解码后解密
     *
     * @param secretKey
     *            秘钥
     * @param iv
     *            偏移向量
     * @param encryptText
     *            密文
     * @return
     * @throws Exception
     */
    public static String decryptStringURLDecoder(String secretKey, String iv,
                                                 String encryptText) throws Exception {
        String retJsonStr = decryptString(secretKey, iv,
                URLDecoder.decode(encryptText));
        return retJsonStr;
    }

    /**
     * URLEncoder编码加密信息
     *
     * @param secretKey
     * @param iv
     * @param plainText
     * @return
     * @throws Exception
     */
    public static String encryptStringURLEncoder(String secretKey, String iv,
                                                 String plainText) throws Exception {
        String base64Str = encryptString(secretKey, iv, plainText);
        return URLEncoder.encode(base64Str);
    }

    public static void main(String[] args) throws Exception {
        //String content = "346000000000000a0哎";
        //System.out.println("加密前长度:" + content.length());
//		String str = ("[{\"INDEX_PAGE\":\"cwzt/cw_index.html\",\"USER_ID\":\"cw_admin\",\"USER_LEVEL\":\"5\"}]");
//		String jm=DES3Util.encrypt(str);
//		System.out.println("加密"+jm);
//		String jmd=URLEncoder.encode(jm);
//		System.out.println("编码"+jmd);
//		String jmdec=URLDecoder.decode(jmd);
//		System.out.println("解码"+jmdec);
//		String jiem=decrypt(jmdec);
//		System.out.println("解密"+jiem);
//		System.out.println(MD5.toMD5("123456"));
        //String uuu = "K0%2F9uD4Gul8%3D";
/*		uuu = URLDecoder.decode(uuu);
		System.out.println(decrypt(uuu));
		String ssss = "8VPyzzeqHDlwAoFRRngqweKLctevg%2B%2FVXVxll4SSmxBPumEOw5n3gtvfMfyJ+y0hUJaax%2BWhGq7JI%2BjmgAbDz5UhhoofZ1pgCksCJ6mXDVU0xZRHnVTNua4iS+zY9tuOyBZucDS9elXYqOQsRRzCBuoVvnEiKNunJdmp9YspeYJbuVdKAZvxCI+DWeo86%2FRpZ75gFmzM4By1T1sN6DT3wKCY8aILO0dUT2MgsdDMqmtD4FYyTTc+XiOjolTd3DBT4GPKpsGNHwuiykK%2Fm1CtAE%2BD8soOaZu5nVJHO5zLY9gwmpOF+fU8mdBzmaT860Ax%2BiKhTQCYQhN4a6iAI8OXec4ae5jEyLsvqJl1WDPqwYut8+T9AwdIXzLFrUZuTG6aPSCoYK40oIZnhEBu0BCB7vnNB6Kg%3D%3D";
		ssss = URLDecoder.decode(ssss);
		System.out.println(decrypt(ssss));
		System.out.println("{\"callback\":\"?\"}");*/


        //String time=decrypt(URLDecoder.decode("fmxu5OnQVA4MLVn0hLQ6x44HyA4gVGIxUrXK%2FEoFUAAAQxkLNohE6OnYv48%2F%20Pm8a"));
        String time=decrypt(URLDecoder.decode("mGBmi4vWMfgEzFZOX32BiB6FLtGONbGTPIouYMo%2B2edhUDtXvAGivvu2sOgq+4pcEJhF3DaUypCZ6oTuwZr4rXw%3D%3D"));
        //fmxu5OnQVA6tb8Bi24bAYHFKPT3zoLL2/eqiqRD1Z1IWZPUNUjeobr+zhOW8 KSCSed7w8NLpM7gWj3hwnRXRxw==
        String  jsonstring = decrypt("fmxu5OnQVA6tb8Bi24bAYHFKPT3zoLL2/eqiqRD1Z1IWZPUNUjeobr+zhOW8 KSCSed7w8NLpM7gWj3hwnRXRxw==");
//
        System.out.println("解密"+time);
        long curms=System.currentTimeMillis();
        String p=DES3Util.encrypt("{\"areaId\":\"999\",\"token\":"+curms+",\"userId\":\"71089334@HQ\"}");
        String oa=URLEncoder.encode(p);
        System.out.println(oa);
    }

}
